package skripsi.GeometriAndroid;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Sprite {

	final static int up=0, right=1, down=2, left=3;
	private int bmp_columns;
	private Bitmap bmp;
	private Rect spriteFrame;
	private int[] frameSequence;
	private GameView gameView;
    private int x;
    private int y;
    private int xSpeed;
    private int ySpeed;
    private int currentFrame = 0;
    private int width;
    private int height;
    private int direction=up;
    private int countFrame=0;

    public Sprite(GameView gameView,Bitmap bmp,int x,int y,int rows,int column,int speed ){
    	this.gameView= gameView;
    	this.x=x;
    	this.y=y;
    	this.bmp_columns=column;
    	this.bmp=bmp;
    	this.xSpeed=speed;
    	this.ySpeed=speed;
    	this.height=bmp.getHeight();
    	this.width=bmp.getWidth();
    	this.spriteFrame=new Rect(0,0,0,0);
    	this.spriteFrame.left=width;
    	this.spriteFrame.right=height;
    }
    	public void setDirection(int direct){
    		this.direction= direct;
    	}
    	public int getDirection(){
    		return  this.direction;
    	}
    	
    	public void setX(int x){
    		this.x=x;
    	}
    	public int getX(){
    		return this.x;
    	}
    	
    	public void setY(int y){
    		this.y=y;
    	}
    	public int getY(){
    		return this.y;
    	}
    	
    	public void setWidth(int width){
    		this.width=width;
    	}
    	public int getWidth(){
    		return this.width;
    	}
    	
    	public void setHeight(int height){
    		this.height=height;
    	}
    	public int getHeight(){
    		return this.height;
    	}
    	 public void setFrameSequence(int[] frameSeq){
    			this.frameSequence=frameSeq;
    			this.currentFrame=frameSequence[0];
    			this.countFrame=0;
    			createFrame(currentFrame);
    		}
    	
    	public void createFrame(int frameNumber){
    		int x=frameNumber%bmp_columns;
    		int y=frameNumber/bmp_columns;
    		this.spriteFrame.left=x*width;
    		this.spriteFrame.right=spriteFrame.left+width;
    		this.spriteFrame.top=y*height;
    		this.spriteFrame.bottom=spriteFrame.top+height;
    	}
    	
    	public void nextFrame(){
    		if(currentFrame<frameSequence.length-1){
    			currentFrame=frameSequence[++countFrame];
    		}
    		else{
    			currentFrame=frameSequence[0];
    			countFrame=0;
    		}
    		createFrame(currentFrame);
    		}
    	
    	public void move(){
    		if(direction==up){
    			if(!(y-ySpeed <= 0)){
    				//cek dahulu apakah mencapai batas atas layar
    				setY(getY()-ySpeed);
    			}
    		}else if(direction==right){
    			if(!(x+width+xSpeed >= gameView.getWidth())){
    				//cek dahulu apakah mencapai batas kanan layar
    				setX(getX()+xSpeed);
    			}
    			
    		}else if(direction==down){
    			if(!(y+height+ySpeed >= gameView.getHeight())){
    				//cek dahulu apakah mencapai batas bawah layar
    				setY(getY()+ySpeed);
    			}
    		}else if(direction==left){
    			if(!(x-xSpeed <= 0)){
    				setX(getX()-xSpeed);
    			}
    		}
    		nextFrame();
    	}
    	
    	public void onDraw(Canvas canvas){
    		Rect dest= new Rect(getX(), getY(), getX()+width, getY()+ height);
    		canvas.drawBitmap(bmp, spriteFrame, dest, null);
    	}
    	
    	  public boolean isCollition(Sprite sprite) {
              if(x>sprite.getX() && x<sprite.getX()+sprite.getWidth() && y>sprite.getY() && y<sprite.getY()+sprite.getHeight()){
            	  return true;
              }
              if(x+width>sprite.getX() && x+width<sprite.getX()+sprite.getWidth() && y>sprite.getY() && y<sprite.getY()+sprite.getHeight()){
            	  return true;
              }
              if(x>sprite.getX() && x<sprite.getX()+sprite.getWidth() && y+height>sprite.getY() && y+height<sprite.getY()+sprite.getHeight()){
            	  return true;
              }
              if(x+width>sprite.getX() && x+width<sprite.getX()+sprite.getWidth() && y+height>sprite.getY() && y+height<sprite.getY()+sprite.getHeight()){
            	  return true;
              }
              return false;
        }
    }
	
